package com.jixu.numberapp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class Threadconfiguration {

    /**
     * 这里的参数一般是从配置文件中读取，我这里就直接写死
     */
    @Bean("TaskExecutor")
    public ThreadPoolTaskExecutor testThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor orderThreadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        //核心线程数量
        orderThreadPoolTaskExecutor.setCorePoolSize(2);
        //最大线程数量
        orderThreadPoolTaskExecutor.setMaxPoolSize(5);
        //队列中最大任务数
        orderThreadPoolTaskExecutor.setQueueCapacity(200);
        //线程名称前缀
        orderThreadPoolTaskExecutor.setThreadNamePrefix("ThreadPool-");
        //线程空闲后最大空闲时间
        orderThreadPoolTaskExecutor.setKeepAliveSeconds(30);
        // 关闭所有线程
        orderThreadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(false);
        //当到达最大线程数是如何处理新任务
        orderThreadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return orderThreadPoolTaskExecutor;
    }

}
